package com.baizhi.action;

import java.util.List;

import com.baizhi.entity.Product;
import com.baizhi.service.ProductService;
import com.baizhi.service.impl.ProductServiceImpl;

public class ProductAction {
	private List<Product> list;
	private Product product;
	private int id;
	private List<Integer> ids;
	private int pageNum;
	private int pageSize = 5;//每页显示多少条
	private int pageMax;//页面最大值

	public int getPageMax() {
		return pageMax;
	}
	public void setPageMax(int pageMax) {
		this.pageMax = pageMax;
	}
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public List<Integer> getIds() {
		return ids;
	}
	public void setIds(List<Integer> ids) {
		this.ids = ids;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	public List<Product> getList() {
		return list;
	}
	public void setList(List<Product> list) {
		this.list = list;
	}
	//展示所有商品
	public String showAll() {
		ProductService ps = new ProductServiceImpl();
		list = ps.selectAll();
		return "success";
	}
	//添加商品
	public String addProduct() {
		ProductService ps = new ProductServiceImpl();
//		product = new Product(1, product.getName(), product.getDescription(), product.getPrice(), product.getAccount(), product.getStatus());
		if(product != null) {
			ps.insert(product);
			return "success";
		}else {
			return "false";
		}
	}
	//删除选中的商品
	public String deleteProduct() {
		ProductService ps = new ProductServiceImpl();
		ps.deleteById(id);
		return "success";
	}
	//根据id查询商品
	public String selectById() {
		ProductService ps = new ProductServiceImpl();
		product = ps.selectById(id);
		System.out.println(product);
		return "success";
	}
	//修改商品信息
	public String updateProduct() {
		ProductService ps = new ProductServiceImpl();
		ps.updateById(product);
		return "success";
	}
	//商品上下架
	public String UpDownProduct() {
		ProductService ps = new ProductServiceImpl();
		product = ps.selectById(id);
		if(product.getStatus() == 0){
			ps.updateById(new Product(product.getId(), product.getName(), product.getDescription(), product.getPrice(), product.getAccount(), 1));
			return "success";
		}else {
			ps.updateById(new Product(product.getId(), product.getName(), product.getDescription(), product.getPrice(), product.getAccount(), 0));
			return "success";
		}
	}
	//删除选中
	public String deleteMore() {
		ProductService ps = new ProductServiceImpl();
		ps.deleteMore(ids);
		return "success";
	}
	//分页
	public String selectByPage(){
		ProductService ps = new ProductServiceImpl();
		ps.selectByPage(pageNum,pageSize);
		return "success";
	}
}
